package com.huluobo.a2206douyindemo.shop;

import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.huluobo.base.bean.CartBeanResult;
import com.huluobo.base.bean.CommonBeanResult;
import com.huluobo.base.bean.GoodsBeanResult;
import com.huluobo.base.mvvm.BaseViewModel;
import com.huluobo.base.net.Const;

import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/8/28.
 */
public class ShopViewModel extends BaseViewModel<ShopRepo> {
    MutableLiveData<GoodsBeanResult> getGoodsSuccess = new MutableLiveData<>();
    MutableLiveData<String> getGoodsFailed = new MutableLiveData<>();

    MutableLiveData<CommonBeanResult> addToCartSuccess = new MutableLiveData<>();
    MutableLiveData<String> addToCartFailed = new MutableLiveData<>();

    MutableLiveData<CartBeanResult> getCartListSuccess = new MutableLiveData<>();
    MutableLiveData<String> getCartListFailed = new MutableLiveData<>();

    MutableLiveData<CommonBeanResult> deleteCartSuccess = new MutableLiveData<>();
    MutableLiveData<String> deleteCartFailed = new MutableLiveData<>();

    public void deleteCart(String ids) {
        Map<String, String> map = new HashMap<>();
        map.put(Const.PARAM_IDS, ids);
        String json = new Gson().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), json);
        repo.deleteCart(body, deleteCartSuccess, deleteCartFailed);
    }

    public void getCartList() {
        repo.getCartList(getCartListSuccess, getCartListFailed);
    }

    public void addToCart(int goodsId, int count) {
        Map<String, Integer> map = new HashMap<>();
        map.put(Const.PARAM_GOODS_ID, goodsId);
        map.put(Const.PARAM_COUNT, count);
        String json = new Gson().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse(Const.MEDIA_TYPE), json);
        repo.addToCart(body, addToCartSuccess, addToCartFailed);
    }

    public void getGoods(int categoryId, int currentPage, int pageSize) {
        repo.getAllGoods(categoryId, currentPage, pageSize, getGoodsSuccess, getGoodsFailed);
    }

    @Override
    protected ShopRepo createRepo() {
        return new ShopRepo();
    }
}
